/*     */ package com.infoservice.schedule.worker;
/*     */ 
/*     */ import com.infoservice.schedule.Constant;
/*     */ import java.util.LinkedList;
/*     */ import org.apache.log4j.LogManager;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class WorkerMng extends Thread
/*     */ {
/*  29 */   private static Logger logger = LogManager.getLogger(WorkerMng.class);
/*  30 */   private static WorkerMng workerMng = null;
/*     */ 
/*  32 */   private LinkedList<Worker> workerPool = new LinkedList();
/*  33 */   private int count = 0;
/*  34 */   private boolean isStop = false;
/*     */ 
/*     */   private WorkerMng(String name) {
/*  37 */     super(name);
/*  38 */     for (int i = 0; i < Constant.WORKER_COUNT; i++) {
/*  39 */       Worker worker = new Worker("Worker-" + this.count);
/*  40 */       this.count += 1;
/*  41 */       this.workerPool.addLast(worker);
/*  42 */       worker.start();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static WorkerMng getInstance() {
/*  47 */     if (workerMng == null)
/*  48 */       synchronized (WorkerMng.class) {
/*  49 */         if (workerMng == null)
/*  50 */           workerMng = new WorkerMng(WorkerMng.class.getSimpleName());
/*     */       }
/*  52 */     return workerMng;
/*     */   }
/*     */ 
/*     */   public synchronized void setWorker(int num) {
/*  56 */     if ((num <= 0) || (num == this.workerPool.size())) return;
/*     */ 
/*  58 */     if (num > this.workerPool.size()) {
/*  59 */       int size = num - this.workerPool.size();
/*  60 */       for (int i = 0; i < size; i++) {
/*  61 */         Worker worker = new Worker("Worker-" + this.count);
/*  62 */         this.count += 1;
/*  63 */         this.workerPool.addLast(worker);
/*  64 */         worker.start();
/*     */       }
/*  66 */       Constant.WORKER_COUNT = this.workerPool.size();
/*  67 */       logger.info("Worker Count : " + this.workerPool.size());
/*  68 */       return;
/*     */     }
/*     */ 
/*  71 */     if (num < this.workerPool.size()) {
/*  72 */       int size = this.workerPool.size() - num;
/*  73 */       for (int i = 0; i < size; i++) {
/*  74 */         Worker worker = (Worker)this.workerPool.removeFirst();
/*  75 */         worker.stopThread();
/*     */       }
/*  77 */       Constant.WORKER_COUNT = this.workerPool.size();
/*  78 */       logger.info("Worker Count : " + this.workerPool.size());
/*  79 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   public void run() {
/*  84 */     while (!this.isStop)
/*     */     {
/*  86 */       logger.info("WorkerMng   [workerPool size = " + getWorkerNum() + " ]");
/*  87 */       logger.debug("Constant.WORKER_STATUS_CHECK_INTERVAL is " + Constant.WORKER_STATUS_CHECK_INTERVAL + " 分钟");
/*  88 */       int size = this.workerPool.size();
/*  89 */       for (int i = size; i > 0; i--) {
/*  90 */         logger.info("Worker State [ " + ((Worker)this.workerPool.get(i - 1)).getName() + " state is " + ((Worker)this.workerPool.get(i - 1)).getState().name() + " ]");
/*     */       }
/*  92 */       synchronized (this) {
/*     */         try {
/*  94 */           wait(Constant.WORKER_STATUS_CHECK_INTERVAL * 60 * 1000);
/*     */         } catch (InterruptedException e) {
/*  96 */           logger.error(e.getMessage(), e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public synchronized void stopWorkMng() {
/* 103 */     this.isStop = true;
/* 104 */     interrupt();
/*     */ 
/* 106 */     for (Worker wk : this.workerPool)
/* 107 */       wk.stopThread();
/*     */   }
/*     */ 
/*     */   public int getWorkerNum()
/*     */   {
/* 112 */     return this.workerPool.size();
/*     */   }
/*     */ }

/* Location:           D:\Workspace\Work\ASMP_DE\lib\frame3\InfoSchedule_1.0.03.jar
 * Qualified Name:     com.infoservice.schedule.worker.WorkerMng
 * JD-Core Version:    0.6.0
 */